package com.example.pclimb.ui.home;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pclimb.R;
import com.example.pclimb.model.TravelGuide;

import java.util.ArrayList;
import java.util.List;

/**
 * 热门攻略列表适配器
 */
public class PopularGuidesAdapter extends RecyclerView.Adapter<PopularGuidesAdapter.GuideViewHolder> {

    private List<TravelGuide> travelGuides = new ArrayList<>();
    private OnItemClickListener listener;

    @NonNull
    @Override
    public GuideViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_travel_guide, parent, false);
        return new GuideViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GuideViewHolder holder, int position) {
        TravelGuide currentGuide = travelGuides.get(position);
        holder.textTitle.setText(currentGuide.getTitle());
        holder.textLocation.setText(currentGuide.getLocation());
        holder.textViewCount.setText(String.valueOf(currentGuide.getViewCount()));
    }

    @Override
    public int getItemCount() {
        return travelGuides.size();
    }

    public void setTravelGuides(List<TravelGuide> travelGuides) {
        this.travelGuides = travelGuides;
        notifyDataSetChanged();
    }

    public TravelGuide getTravelGuideAt(int position) {
        return travelGuides.get(position);
    }

    class GuideViewHolder extends RecyclerView.ViewHolder {
        private TextView textTitle;
        private TextView textLocation;
        private TextView textViewCount;

        public GuideViewHolder(@NonNull View itemView) {
            super(itemView);
            textTitle = itemView.findViewById(R.id.text_guide_title);
            textLocation = itemView.findViewById(R.id.text_guide_location);
            textViewCount = itemView.findViewById(R.id.text_view_count);

            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(travelGuides.get(position));
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(TravelGuide travelGuide);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
} 